package org.dromara.mp.service.impl;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.dromara.mp.service.WxMpQrCodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 微信公众号二维码生成服务实现类
 *
 * @author ruoyi
 */
@Service
public class WxMpQrCodeServiceImpl implements WxMpQrCodeService {

    private static final Logger logger = LoggerFactory.getLogger(WxMpQrCodeServiceImpl.class);

    @Autowired
    private WxMpService wxMpService;

    @Override
    public WxMpQrCodeTicket createTempQrCode(int sceneId, int expireSeconds) {
        try {
            WxMpQrCodeTicket ticket = wxMpService.getQrcodeService().qrCodeCreateTmpTicket(sceneId, expireSeconds);
            logger.info("创建临时二维码成功: sceneId={}, ticket={}", sceneId, ticket.getTicket());
            return ticket;
        } catch (WxErrorException e) {
            logger.error("创建临时二维码失败: {}", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public WxMpQrCodeTicket createPermanentQrCode(String sceneStr) {
        try {
            WxMpQrCodeTicket ticket = wxMpService.getQrcodeService().qrCodeCreateLastTicket(sceneStr);
            logger.info("创建永久二维码成功: sceneStr={}, ticket={}", sceneStr, ticket.getTicket());
            return ticket;
        } catch (WxErrorException e) {
            logger.error("创建永久二维码失败: {}", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public WxMpQrCodeTicket createPermanentQrCode(int sceneId) {
        try {
            WxMpQrCodeTicket ticket = wxMpService.getQrcodeService().qrCodeCreateLastTicket(sceneId);
            logger.info("创建永久二维码成功: sceneId={}, ticket={}", sceneId, ticket.getTicket());
            return ticket;
        } catch (WxErrorException e) {
            logger.error("创建永久二维码失败: {}", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public WxMpQrCodeTicket createForeverQrCode(String sceneStr) {
        // 别名方法，直接调用createPermanentQrCode
        return createPermanentQrCode(sceneStr);
    }

    @Override
    public String getQrCodeUrl(String ticket) {
        String url = null;
        try {
            url = wxMpService.getQrcodeService().qrCodePictureUrl(ticket);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
        logger.info("获取二维码URL成功: ticket={}, url={}", ticket, url);
        return url;
    }

    @Override
    public WxMpQrCodeTicket createUserInviteQrCode(String userId) {
        String sceneStr = "invite_" + userId;
        return createPermanentQrCode(sceneStr);
    }

    @Override
    public WxMpQrCodeTicket createActivityQrCode(String activityId) {
        String sceneStr = "activity_" + activityId;
        return createPermanentQrCode(sceneStr);
    }

    @Override
    public WxMpQrCodeTicket createChannelQrCode(String channelId) {
        String sceneStr = "channel_" + channelId;
        return createPermanentQrCode(sceneStr);
    }
}